//  service/user.js
'use strict';
const Service = require('egg').Service;

class UserService extends Service {


  async findAll() {
    const result = await this.app.mysql.select('whjdis');
    return {
      code: 200,
      msg: '查找成功',
      data: result,
    };
  }

  async findAlls() {
    const result = await this.app.mysql.select('user');
    return {
      code: 200,
      msg: '查找成功',
      data: result,
    };
  }

  async find(uid) {

    const result = await this.app.mysql.get('whjdis', { id: uid });
    if (result !== null) {
      return {
        code: 200,
        msg: '查找成功',
        data: result,
      };
    }
    return {
      msg: '没有此条数据！',
    };

  }

  async create(params) {
    const cfg = {
      id: params.id,
      name: params.name,
      age: params.age,
      img: params.img,
    };
    const result = await this.app.mysql.insert('whjdis', cfg);
    return {
      code: 200,
      msg: '创建成功',
      data: result,
    };
  }
  async delete(uid) {
    const result = await this.app.mysql.delete('whjdis', { id: uid });
    return {
      code: 200,
      msg: '删除成功',
      data: result,
    };
  }

  async update(uid, params) {
    const cfg = {
      ...params,
    };
    const result = await this.app.mysql.update('whjdis', cfg);
    return {
      code: 200,
      msg: '更新成功',
      data: result,
    };
  }
  async updates(uid, params) {
    const cfg = {
      ...params,
    };
    const result = await this.app.mysql.update('user', cfg);
    return {
      code: 200,
      msg: '更新成功',
      data: result,
    };
  }

  async getUserInfo(params) {
    const { app } = this;
    try {
      const result = await app.mysql.get('user', params);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 通过用户名获取用户信息
  async getUserByName(username) {
    const { app } = this;
    try {
      const result = await app.mysql.get('user', { username });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async register(params) {
    const { app } = this;
    try {
      const result = await app.mysql.insert('user', params);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}
module.exports = UserService;

